<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="闲置物名称" prop="idleName">
      <el-input v-model="dataForm.idleName" placeholder="闲置物名称"></el-input>
    </el-form-item>
    <el-form-item label="详情" prop="idleDetails">
      <el-input v-model="dataForm.idleDetails" placeholder="详情"></el-input>
    </el-form-item>
    <el-form-item label="图集" prop="pictureList">
      <el-input v-model="dataForm.pictureList" placeholder="图集"></el-input>
    </el-form-item>
    <el-form-item label="价格" prop="idlePrice">
      <el-input v-model="dataForm.idlePrice" placeholder="价格"></el-input>
    </el-form-item>
    <el-form-item label="发货地区" prop="idlePlace">
      <el-input v-model="dataForm.idlePlace" placeholder="发货地区"></el-input>
    </el-form-item>
    <el-form-item label="分类标签" prop="idleLabel">
      <el-input v-model="dataForm.idleLabel" placeholder="分类标签"></el-input>
    </el-form-item>
    <el-form-item label="发布时间" prop="releaseTime">
      <el-input v-model="dataForm.releaseTime" placeholder="发布时间"></el-input>
    </el-form-item>
    <el-form-item label="状态（发布1、下架2、删除0）" prop="idleStatus">
      <el-input v-model="dataForm.idleStatus" placeholder="状态（发布1、下架2、删除0）"></el-input>
    </el-form-item>
    <el-form-item label="用户主键id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户主键id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          idleName: '',
          idleDetails: '',
          pictureList: '',
          idlePrice: '',
          idlePlace: '',
          idleLabel: '',
          releaseTime: '',
          idleStatus: '',
          userId: ''
        },
        dataRule: {
          idleName: [
            { required: true, message: '闲置物名称不能为空', trigger: 'blur' }
          ],
          idleDetails: [
            { required: true, message: '详情不能为空', trigger: 'blur' }
          ],
          pictureList: [
            { required: true, message: '图集不能为空', trigger: 'blur' }
          ],
          idlePrice: [
            { required: true, message: '价格不能为空', trigger: 'blur' }
          ],
          idlePlace: [
            { required: true, message: '发货地区不能为空', trigger: 'blur' }
          ],
          idleLabel: [
            { required: true, message: '分类标签不能为空', trigger: 'blur' }
          ],
          releaseTime: [
            { required: true, message: '发布时间不能为空', trigger: 'blur' }
          ],
          idleStatus: [
            { required: true, message: '状态（发布1、下架2、删除0）不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户主键id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/trading/idleitem/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.idleName = data.idleItem.idleName
                this.dataForm.idleDetails = data.idleItem.idleDetails
                this.dataForm.pictureList = data.idleItem.pictureList
                this.dataForm.idlePrice = data.idleItem.idlePrice
                this.dataForm.idlePlace = data.idleItem.idlePlace
                this.dataForm.idleLabel = data.idleItem.idleLabel
                this.dataForm.releaseTime = data.idleItem.releaseTime
                this.dataForm.idleStatus = data.idleItem.idleStatus
                this.dataForm.userId = data.idleItem.userId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/trading/idleitem/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'idleName': this.dataForm.idleName,
                'idleDetails': this.dataForm.idleDetails,
                'pictureList': this.dataForm.pictureList,
                'idlePrice': this.dataForm.idlePrice,
                'idlePlace': this.dataForm.idlePlace,
                'idleLabel': this.dataForm.idleLabel,
                'releaseTime': this.dataForm.releaseTime,
                'idleStatus': this.dataForm.idleStatus,
                'userId': this.dataForm.userId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
